﻿using MongoDB.Driver;
using Sustainalytics.ClientDW.Entities;
using System.Collections.Generic;

namespace Sustainalytics.ScreeningTool.Synchronization.Handlers
{
    public class CarbonDataHandler : IHandler<CarbonData>
    {
        public ProjectionDefinition<CarbonData> GetProjection(ProjectionDefinitionBuilder<CarbonData> builder) => builder
            .Include(x => x.Emissions)
            .Include(x => x.RiskRating)
            .Include(x => x.FossilFuel)
            .Include(x => x.Solutions)
            .Include(x => x.StrandedAssets);

        public IEnumerable<ExtraElement> GetExtraElements(CarbonData entity)
            => EnumerableExtensions.Concat(
                entity.Emissions?.GetExtraElements(),
                entity.RiskRating?.GetExtraElements(),
                entity.FossilFuel?.GetExtraElements(),
                entity.Solutions?.GetExtraElements(),
                entity.StrandedAssets?.GetExtraElements());
    }

    public static class CarbonDataExtensions
    {
        public static IEnumerable<ExtraElement> GetExtraElements(this CarbonEmissions entity) => new ExtraElementList
        {
            { "Carbon - Total Revenues (mln. USD)", entity.TotalRevenue                  },
            { "Carbon - Scope 1 Emissions",         entity.Scope1                        },
            { "Carbon - Scope 2 Emissions",         entity.Scope2                        },
            { "Carbon - Total Emissions",           entity.Total                         },
            { "Carbon - Emissions Source",          entity.Source                        },
            { "Carbon - Carbon Intensity",          entity.CarbonIntensity               },
            { "Carbon - Intensity Percentile Rank", entity.CarbonIntensityPercentileRank },
            { "Carbon - Intensity Group",           entity.CarbonIntensityGroup          },
            { "Carbon - Financial Year",            entity.FinancialYear                 },
        };

        public static IEnumerable<ExtraElement> GetExtraElements(this CarbonRiskRating entity) => new ExtraElementList
        {
            { "Carbon - Overall-Risk Score",                           entity.RiskScore                               },
            { "Carbon - Overall-Exposure Score",                       entity.ExposureScore                           },
            { "Carbon - Overall-Management Score",                     entity.ManagementScore                         },
            { "Carbon - Overall-Risk Category",                        entity.RiskCategory                            },
            { "Carbon - Operations-Risk Score",                        entity.OperationsRiskScore                     },
            { "Carbon - Operations-Exposure Score",                    entity.OperationsExposureScore                 },
            { "Carbon - Operations-Management Score",                  entity.OperationsManagementScore               },
            { "Carbon - Products and Services-Risk Score",             entity.ProductsAndServicesRiskScore            },
            { "Carbon - Products and Services-Exposure Score",         entity.ProductsAndServicesExposureScore        },
            { "Carbon - Products and Services-Management Score",       entity.ProductsAndServicesManagementScore      },
            { "Carbon - Overall-Managed Risk Score",                   entity.OverallManagedRiskScore                 },
            { "Carbon - Operations-Managed Risk Score",                entity.OperationsManagedRiskScore              },
            { "Carbon - Products and Services-Managed Risk Score",     entity.ProductsAndServicesManagedRiskScore     },
            { "Carbon - Overall-Manageable Risk Score",                entity.OverallManageableRiskScore              },
            { "Carbon - Operations-Manageable Risk Score",             entity.OperationsManageableRiskScore           },
            { "Carbon - Products and Services-Manageable Risk Score",  entity.ProductsAndServicesManageableRiskScore  },
            { "Carbon - Operations-Default Exposure Score",            entity.OperationsDefaultExposureScore          },
            { "Carbon - Products and Services-Default Exposure Score", entity.ProductsAndServicesDefaultExposureScore },
        };

        public static IEnumerable<ExtraElement> GetExtraElements(this CarbonFossilFuel entity) => new ExtraElementList
        {
            { "Carbon - Thermal Coal Extraction-Level of Involvement Id",              entity.ThermalCoalExtraction.Id              },
            { "Carbon - Thermal Coal Extraction-Level of Involvement Range",           entity.ThermalCoalExtraction.Range           },
            { "Carbon - Thermal Coal Power Generation-Level of Involvement Id",        entity.ThermalCoalPowerGeneration.Id         },
            { "Carbon - Thermal Coal Power Generation-Level of Involvement Range",     entity.ThermalCoalPowerGeneration.Range      },
            { "Carbon - Oil Sands Extraction-Level of Involvement Id",                 entity.OilSandsExtraction.Id                 },
            { "Carbon - Oil Sands Extraction-Level of Involvement Range",              entity.OilSandsExtraction.Range              },
            { "Carbon - Arctic Oil and Gas Exploration Involvement",                   entity.ArcticOilAndGasExplorationInvolvement },
            { "Carbon - Oil and Gas Generation-Level of Involvement Id",               entity.OilGasGeneration.Id                   },
            { "Carbon - Oil and Gas Generation-Level of Involvement Range",            entity.OilGasGeneration.Range                },
            { "Carbon - Oil and Gas Production-Level of Involvement Id",               entity.OilGasProduction.Id                   },
            { "Carbon - Oil and Gas Production-Level of Involvement Range",            entity.OilGasProduction.Range                },
            { "Carbon - Oil and Gas Products and Services-Level of Involvement Id",    entity.OilGasProductsServices.Id             },
            { "Carbon - Oil and Gas Products and Services-Level of Involvement Range", entity.OilGasProductsServices.Range          },
            { "Carbon - Fossil Fuel-Level of Involvement Id",                          entity.FossilFuelInvolvement.Id              },
            { "Carbon - Fossil Fuel-Level of Involvement Range",                       entity.FossilFuelInvolvement.Range           },
        };

        public static IEnumerable<ExtraElement> GetExtraElements(this CarbonSolutions entity) => new ExtraElementList
        {
            { "Carbon - Renewable Energy Production-Level of Involvement Id",                     entity.RenewableEnergyProduction.Id                   },
            { "Carbon - Renewable Energy Production-Level of Involvement Range",                  entity.RenewableEnergyProduction.Range                },
            { "Carbon - Renewable Energy Supporting Product Services-Level of Involvement Id",    entity.RenewableEnergySupportingProductServices.Id    },
            { "Carbon - Renewable Energy Supporting Product Services-Level of Involvement Range", entity.RenewableEnergySupportingProductServices.Range },
            { "Carbon - Green Transportation-Level of Involvement Id",                            entity.GreenTransportation.Id                         },
            { "Carbon - Green Transportation-Level of Involvement Range",                         entity.GreenTransportation.Range                      },
            { "Carbon - Carbon Solutions-Level of Involvement Id",                                entity.Involvement.Id                                 },
            { "Carbon - Carbon Solutions-Level of Involvement Range",                             entity.Involvement.Range                              },
        };

        public static IEnumerable<ExtraElement> GetExtraElements(this CarbonStrandedAssets entity) => new ExtraElementList
        {
            { "Carbon - Stranded Assets-Exposure Score",                 entity.ExposureScore                              },
            { "Carbon - Oil and Gas Reserves (MMBOE)",                   entity.OilAndGasReserves                          },
            { "Carbon - Oil and Gas Production (MMBOE)",                 entity.OilAndGasProduction                        },
            { "Carbon - Weighted Lifecycle Intensity-Score",             entity.WeightedLifecycleIntensityScore            },
            { "Carbon - Weighted Lifecycle Intensity",                   entity.WeightedLifecycleIntensity                 },
            { "Carbon - Reserve Lifecycle Intensity",                    entity.ReserveLifecycleIntensity                  },
            { "Carbon - Production Lifecycle Intensity",                 entity.ProductionLifecycleIntensity               },
            { "Carbon - Total Oil & Gas Reserves",                       entity.TotalOilAndGasReserves                     },
            { "Carbon - Total Crude Oil Reserves",                       entity.TotalCrudeOilReserves                      },
            { "Carbon - Total Bitumen & Synthetic Crude Oil Reserves",   entity.TotalBitumenAndSyntheticCrudeOilReserves   },
            { "Carbon - Total Natural Gas Reserves",                     entity.TotalNaturalGasReserves                    },
            { "Carbon - Total Natural Gas Liquids Reserves",             entity.TotalNaturalGasLiquidsReserves             },
            { "Carbon - Total Oil & Gas Production",                     entity.TotalOilAndGasProduction                   },
            { "Carbon - Total Oil Production",                           entity.TotalOilProduction                         },
            { "Carbon - Total Bitumen & Synthetic Crude Oil Production", entity.TotalBitumenAndSyntheticCrudeOilProduction },
            { "Carbon - Total Natural Gas Production",                   entity.TotalNaturalGasProduction                  },
            { "Carbon - Total Natural Gas Liquid Production",            entity.TotalNaturalGasLiquidProduction            },
            { "Carbon - Weighted High-Cost Production",                  entity.WeightedHighCostProduction                 },
        };
    }
}
